package com.lxmes.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lxmes.base.service.IJcIteminfoService;
import com.lxmes.base.service.IJcWarehouseinfoKuweiService;
import com.lxmes.business.domain.CcCaigouMain;
import com.lxmes.business.domain.CcChukuMain;
import com.lxmes.business.service.ICcRukuDetailService;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.system.service.ISysDictDataService;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.business.domain.CcRukuMain;
import com.lxmes.business.service.ICcRukuMainService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 入库任务Controller
 * 
 * @author 张志煜
 * @date 2022-07-03
 */
@Api(tags="入库单")
@RestController
@RequestMapping("/business/rukumain")
public class CcRukuMainController extends BaseController
{
    @Autowired
    private ICcRukuMainService ccRukuMainService;
    @Autowired
    private ICcRukuDetailService ccRukuDetailService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private IJcWarehouseinfoKuweiService kuweiService;
    @Autowired
    private IJcIteminfoService jcIteminfoService;
    /**
     * 查询入库任务列表
     */
    @ApiOperation("入库主表列表")
    @PreAuthorize("@ss.hasPermi('business:rukumain:list')")
    @GetMapping("/list")
    public TableDataInfo list(CcRukuMain ccRukuMain)
    {
        startPage();
        ccRukuMain.setDeptId(SecurityUtils.getDeptId());
        List<CcRukuMain> list = ccRukuMainService.selectCcRukuMainList(ccRukuMain);
        return getDataTable(list);
    }



    /**
     * 手机端入库单列表
     */
    @ApiOperation(value="入库单列表",tags = "rukuFlag入库方式（1-原材料入库 2-产成品入库）")
    //查询条件
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryParams", value = "入库单号、经办人", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "rukuFlag", value = "入库方式", paramType="query", dataType = "String",required = true,dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageSize", value = "分页大小",  required = true,paramType="query",  dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNum", value = "当前页数",  required = true,paramType="query",   dataType = "int", dataTypeClass = Integer.class)
    })
    @ApiResponses({@ApiResponse(code=200,message = "0k",response = CcRukuMain.class)})
    @PreAuthorize("@ss.hasPermi('mobile:rukumain:list')")
    @GetMapping("/mobileList")
    public TableDataInfo mobileList(@RequestParam(value ="queryParams",required = false) String queryParams,@RequestParam(value ="rukuFlag") String rukuFlag)
    {
        startPage();
        CcRukuMain ccRukuMain=new CcRukuMain();
        ccRukuMain.setQueryParams(queryParams);
        ccRukuMain.setRukuFlag(rukuFlag);
        List<CcRukuMain> list = ccRukuMainService.selectCcRukuMainList(ccRukuMain);
        return getDataTable(list);
    }
    /**
     * 手机端新增入库
     */
    @ApiOperation(value="新增入库任务",tags = "rukuFlag入库方式（1-原材料入库 2-产成品入库）")
    @Log(title = "新增入库任务", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('mobile:rukumain:mobileAdd')")
    @PostMapping("/mobileAdd")
    public AjaxResult mobileAdd(@RequestBody CcRukuMain ccRukuMain)
    {
        return toAjax(ccRukuMainService.insertCcRukuMain(ccRukuMain));
    }
    @ApiOperation(value="获取入库类型")
    @GetMapping("/getDictDataRuKuType")
    public AjaxResult getDictDataRuKuType(){
        return AjaxResult.success( dictDataService.selectDictDataByType("ruku_type"));
    }
    @ApiOperation(value="获取入库单位")
    @GetMapping("/getDictDataRuKuUnit")
    public AjaxResult getDictDataRuKuUnit(){
        return AjaxResult.success( dictDataService.selectDictDataByType("jc_iteminfo_unit"));
    }

    /**
     * 导出入库任务列表
     */
    @PreAuthorize("@ss.hasPermi('business:rukumain:export')")
    @Log(title = "入库任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CcRukuMain ccRukuMain)
    {
        List<CcRukuMain> list = ccRukuMainService.selectCcRukuMainList(ccRukuMain);
        ExcelUtil<CcRukuMain> util = new ExcelUtil<CcRukuMain>(CcRukuMain.class);
        util.exportExcel(response, list, "入库任务数据");
    }

    /**
     * 获取入库任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:rukumain:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ccRukuMainService.selectCcRukuMainById(id));
    }
    /**
     * 手机端获取入库单详细信息
     */
    @ApiOperation(value="入库单详细信息")
    @ApiImplicitParam(name = "danhao", value = "入库单号", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @GetMapping(value = "/getMobileRuKuMain/{danhao}")
    public AjaxResult mobileGetInfo(@PathVariable("danhao") String  danhao)
    {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("rukuMain",ccRukuMainService.selectCcRukuMainByDanHao(danhao));
        jsonObject.put("rukuDetail", ccRukuDetailService.selectCcRukuDetailByDanHao(danhao));
        return AjaxResult.success(jsonObject);
    }

    /**
     * 新增入库任务
     */
    @PreAuthorize("@ss.hasPermi('business:rukumain:add')")
    @Log(title = "入库任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CcRukuMain ccRukuMain)
    {
        return toAjax(ccRukuMainService.insertCcRukuMain(ccRukuMain));
    }
    /**
     * 修改入库任务
     */
    @PreAuthorize("@ss.hasPermi('business:rukumain:edit')")
    @Log(title = "入库任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CcRukuMain ccRukuMain)
    {
        return toAjax(ccRukuMainService.updateCcRukuMain(ccRukuMain));
    }
    /**
     * 获取入库任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:rukumain:zhixing')")
    @GetMapping(value = "/zhixingRuku/{danhao}")
    public AjaxResult zhixingRuku(@PathVariable("danhao") String danhao)
    {
        return AjaxResult.success(ccRukuMainService.zhixingRuku(danhao));
    }
    @GetMapping("/getRukumainByRukuFlag/{rukuFlag}")
    public AjaxResult list(@PathVariable("rukuFlag") String rukuFlag)
    {
        List<CcRukuMain> list = ccRukuMainService.selectCcRukuMainByRukuFlag(rukuFlag);
        JSONArray jsonArray=new JSONArray();
        for (CcRukuMain main:list) {
            JSONObject object=new JSONObject();
            object.put("value",main.getDanhao());
            object.put("label",main.getLot());
            //object.put("cusName",main.getSupplyName());
            //object.put("cusId",main.getSupplyId());
            object.put("money",main.getTotalPrice());
            jsonArray.add(object);
        }
        return AjaxResult.success(jsonArray);
    }


    /**
     * 删除入库任务
     */
    @PreAuthorize("@ss.hasPermi('business:rukumain:remove')")
    @Log(title = "入库任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{danhao}")
    public AjaxResult remove(@PathVariable String danhao)
    {
        return toAjax(ccRukuMainService.deleteCcRukuMainByDanhao(danhao));
    }
}
